package com.ayg.controller;

import com.ayg.service.FileService;
import com.ayg.vo.ImageVO;
import com.ayg.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 图片上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public SysResult uploadFile(MultipartFile file){
        ImageVO imageVO = fileService.upload(file);
        //如果imageVO为null  说明文件上传失败
        if(imageVO == null){
            return SysResult.fail();
        }
        return SysResult.success(imageVO);
    }

    /**
     * 实现文件删除
     * URL: /file/deleteFile
     * 请求参数: virtualPath
     * 返回值: SysResult对象
     */
    @DeleteMapping("/deleteFile")
    public SysResult deleteFile(String virtualPath){
        File file = new File(virtualPath);
        //删除指定文件
        file.delete();
        return SysResult.success();
    }

}
